/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import request from '@/utils/request';
import { message } from 'antd';

export async function getLeagueFeeApplyLog(params: {
  keywords?: string;
  current?: number;
  pageSize?: number;
}) {
  const { current, ...rest } = params;
  return request('/Inn/leagueFeeApplyLog', {
    method: 'POST',
    data: {
      page: current,
      ...rest,
    },
  }).then(({ code, data, msg }) => {
    const list = (Array.isArray(data?.list) ? data.list : []).map((item: any, index: number) => ({
      ...item,
      order: data.total - 15 * ((current || 1) - 1) - index,
    }));
    if (code == 0) {
      return {
        data: list,
        total: data.total,
      };
    } else {
      message.error(msg);
      return {
        data: [],
        total: '',
      };
    }
  });
}

export async function setLeagueApplyCheck(params: {
  id?: string;
  check_status: number;
  then?: () => void;
}) {
  const { then = () => {}, ...rest } = params;

  return request('/Inn/leagueApplyCheck', {
    method: 'POST',
    data: { ...rest, check_reason: '' },
  }).then(({ code, msg }) => {
    if (code == 0) {
      message.info(rest.check_status == 1 ? '已通过' : '已拒绝');
      then();
    } else {
      message.error(msg);
    }
  });
}

export async function getLeaguePostData(params: { current?: number; pageSize?: number }) {
  const { current, ...rest } = params;
  return request('/Inn/getLeaguePostData', {
    method: 'POST',
    data: {
      page: current,
      ...rest,
    },
  }).then(({ code, data, msg }) => {
    const list = Array.isArray(data?.list) ? data.list : [];
    if (code == 0) {
      return {
        data: list,
        total: data.total,
      };
    } else {
      message.error(msg);
      return {
        data: [],
        total: '',
      };
    }
  });
}

export async function pushToLeague(params: { id?: string; then?: () => void }) {
  const { then = () => {}, ...rest } = params;
  return request('/Inn/leagueApplyCheck', {
    method: 'POST',
    data: { ...rest },
  }).then(({ code, msg }) => {
    if (code == 0) {
      message.info('成功');
      then();
    } else {
      message.error(msg);
    }
  });
}
export async function downloadLeaguePostData() {
  return request('/Inn/downloadLeaguePostData', {
    method: 'POST',
  }).then(({ code, msg, data }) => {
    if (code == 0 && data) {
      window.open(data);
      message.info('成功');
    } else {
      message.error(msg);
    }
  });
}

export async function getAreaMangeList(params: {
  keywords?: string;
  current?: number;
  pageSize?: number;
}) {
  const { current, ...rest } = params;
  return request('/Inn/indexJson', {
    method: 'POST',
    data: {
      page: current,
      ...rest,
    },
  }).then(({ code, data, msg }) => {
    const list = (Array.isArray(data?.result) ? data.result : []).map(
      (item: any, index: number) => ({
        ...item,
        order: ((current || 1) - 1) * (params.pageSize || 0) + index + 1,
      }),
    );
    if (code == 0) {
      return {
        data: list,
        total: data.total,
      };
    } else {
      message.error(msg);
      return {
        data: [],
        total: '',
      };
    }
  });
}

export async function addRegionAdmin(params: any) {
  return request('/Inn/addRegionAdmin', {
    method: 'POST',
    data: params,
  });
}

export async function areaManageDetail(params: { id: string }) {
  return request('/Inn/indexDetail', {
    method: 'POST',
    data: params,
  });
}
